home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 283_01 / crypt.c < prev    next >
C/C++ Source or Header  |  1988-05-12  |  3KB  |  133 lines

  1.  
  2. /* crypt.c
  3. ** December 30, 1986
  4. */
  5.  
  6. /*=======================================================================*/
  7. /*  An Elementary Holocryptic Cipher:                                    */
  8. /*  The following is a variation on what was described as Hogg's code    */
  9. /*  from World War I in the Sept. 1984 FOGHORN (First Osborne Group).    */
  10. /*  The point of the additional bells & whistles is to make the entire   */
  11. /*  text of the Zaphod Adventure as nearly holocryptic as possible.      */
  12. /*  It works by spinning the key modulus on successive lines; so, while  */
  13. /*  line appearance is preserved, analysis of the characters of each     */
  14. /*  line depends on recovering the initial LCG seed, as well as the key. */ 
  15. /*  Bright adventurers are discouraged from trying to cheat by dumping   */
  16. /*  the Zaphod files, hopefully.  -D. C. Oshel, 10/6/84                  */
  17. /*=======================================================================*/
  18.  
  19. /*
  20.         USAGE:   set_crypt(keystring, seedinteger);  < string ciphers >
  21.                  encode( target1string );
  22.                  encode( target2string );
  23.                  ... etc.
  24.         
  25.                  set_crypt(keystring, seedinteger);
  26.                  decode( target1string );
  27.                  decode( target2string );
  28.                  ... etc.
  29.  
  30.         OR:      set_crypt(key, seed);   < single-character ciphers >
  31.                  c0 = cryptic(c0); 
  32.                  c1 = cryptic(c1); 
  33.                  ... ; 
  34.                  cN = cryptic(cN); 
  35.  
  36.                  set_crypt(key, seed);
  37.                  c0 = uncrypt(c0); 
  38.                  c1 = uncrypt(c1); 
  39.                  ... ; 
  40.                  cN = uncrypt(cN); 
  41. */
  42.  
  43. #include <stdio.h>
  44. #include <string.h>
  45.  
  46. static unsigned int kRNDlxo;
  47. static int kloc, klen;
  48. static char kQey[32];
  49.  
  50. static void Krandi() 
  51. {
  52.         kRNDlxo *= 2053;      /* a long-period LCG */
  53.         kRNDlxo += 13849;
  54.         kloc = abs(kRNDlxo);
  55.         kloc %= klen;
  56. }
  57.  
  58.  
  59.  
  60. void set_crypt( key, seed ) 
  61. char *key; 
  62. int seed; 
  63. {
  64.         strncpy( kQey, key, 31 );
  65.         kRNDlxo = seed; 
  66.         klen = strlen( kQey ); 
  67.         Krandi();
  68. }
  69.  
  70.  
  71.  
  72. int cryptic( c ) 
  73. int c; 
  74. {
  75.         static int k;
  76.         k = c;
  77.         if ( k > 31 && k < 127 ) 
  78.         {
  79.             k = ( k - 32 ) + ( kQey[ kloc ] - 32 );
  80.             k %= 95;
  81.             ++kloc;
  82.             kloc %= klen;
  83.             k += 32;
  84.         }
  85.         return ( k );
  86. }
  87.  
  88.  
  89.  
  90. int uncrypt( c ) 
  91. int c; 
  92. {
  93.         static int k;
  94.         k = c;
  95.         if ( k > 31 && k < 127 ) { 
  96.                 k -= 32; 
  97.                 k -= ( kQey[ kloc ] - 32 ); 
  98.                 ++kloc;
  99.                 kloc %= klen;
  100.                 if ( k < 0 ) k += 95;
  101.                 k += 32;
  102.         }
  103.         return ( k ); 
  104. }
  105.  
  106.  
  107.  
  108. char *encode(p) 
  109. char *p; 
  110. {
  111.         static char *q;
  112.         q = p;
  113.  
  114.         while (*q) { *q = cryptic(*q); q++; }
  115.         Krandi();
  116.         return (p);
  117. }
  118.  
  119.  
  120. char *decode(p) 
  121. char *p; 
  122. {
  123.         static char *q;
  124.         q = p;
  125.  
  126.         while (*q) { *q = uncrypt(*q); q++; }
  127.         Krandi();
  128.         return (p);
  129. }
  130.  
  131.  
  132.  
  133.